package com.ynet.middleground.approve.listener;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.impl.persistence.entity.VariableInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.context.annotation.DependsOn;

import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.approve.cmd.SpringContextUtils;
import com.ynet.middleground.approve.constant.ProcessStatusEnum;
import com.ynet.middleground.approve.constant.WorkflowConstants;
import com.ynet.middleground.approve.model.ProcessModel;
import com.ynet.middleground.approve.model.ProcessRuntimeModel;

/**
 * @author liulx
 * @description 监听服务基础公共服务
 * @date 2019-11-27 16:08
 */
@DependsOn("springContextUtils")
public class BaseListener {

    protected ProcessRuntimeModel processRuntimeModel = SpringContextUtils.getBean(ProcessRuntimeModel.class);
    protected ProcessModel processModel = SpringContextUtils.getBean(ProcessModel.class);

    /**
     * 流程启动和完成是记录
     * 
     * @param delegateExecution
     *            监听执行对象
     * @param statusEnum
     *            流程状态
     */
    protected void startAndEndNotify(DelegateExecution delegateExecution, ProcessStatusEnum statusEnum) {
        String channel = getVariableInstance(delegateExecution, WorkflowConstants.CHANNEL_SIGNAL);
        String startUserId = getVariableInstance(delegateExecution, WorkflowConstants.START_USER_ID);
        ProcessDefinition processDefinitionInfo =
            processModel.getProcessDefinitionInfo(delegateExecution.getProcessDefinitionId());
        String processDefinitionName = processDefinitionInfo.getName();

        if (ProcessStatusEnum.ACTIVE == statusEnum) {
            IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(), "流程启动，[{}]用户通过渠道[{}]发起了[{}]流程",
                startUserId, channel, processDefinitionName);
        } else if (ProcessStatusEnum.COMPLETED == statusEnum) {
            IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(),
                "流程结束，[{}]用户通过渠道[{}]发起的[{}]流程，已经完成", startUserId, channel, processDefinitionName);
        }
    }

    /**
     * 通过执行对象获取流程变量值
     * 
     * @param delegateExecution
     *            执行对象
     * @param variableName
     *            变量名称
     * @return 变量值
     */
    protected String getVariableInstance(DelegateExecution delegateExecution, String variableName) {
        VariableInstance variableInstance = delegateExecution.getVariableInstance(variableName);
        if (variableInstance != null && variableInstance.getValue() != null) {
            return variableInstance.getValue().toString();
        }
        return "";
    }

    /**
     * 通过执行对象获取流程变量值
     *
     * @param delegateExecution
     *            执行对象
     * @param variableName
     *            变量名称
     * @return 变量值
     */
    protected Object getObjectVariableInstance(DelegateExecution delegateExecution, String variableName) {
        VariableInstance variableInstance = delegateExecution.getVariableInstance(variableName);
        if (variableInstance != null && variableInstance.getValue() != null) {
            return variableInstance.getValue();
        }
        return null;
    }

    /**
     * 通过执行对象获取流程变量渠道
     * 
     * @param delegateExecution
     *            执行对象
     * @return 请求渠道
     */
    public String getChannel(DelegateExecution delegateExecution) {
        return getVariableInstance(delegateExecution, WorkflowConstants.CHANNEL_SIGNAL);
    }

    /**
     * 通过执行对象获取流程变量操作人
     * 
     * @param delegateExecution
     *            执行对象
     * @return 操作人id
     */
    public String getOperationUserId(DelegateExecution delegateExecution) {
        return getVariableInstance(delegateExecution, WorkflowConstants.OPERATION_USER_ID);
    }

    /**
     * 通过执行对象获取流程变量流程发起人
     * 
     * @param delegateExecution
     *            执行对象
     * @return 流程发起人id
     */
    public String getStartUserId(DelegateExecution delegateExecution) {
        return getVariableInstance(delegateExecution, WorkflowConstants.START_USER_ID);
    }

    /**
     * 通过执行对象获取流程变量值
     *
     * @param delegateTask
     *            执行对象
     * @param variableName
     *            变量名称
     * @return 变量值
     */
    protected String getVariableInstance(DelegateTask delegateTask, String variableName) {
        VariableInstance variableInstance = delegateTask.getVariableInstance(variableName);
        if (variableInstance != null && variableInstance.getValue() != null) {
            return variableInstance.getValue().toString();
        }
        return "";
    }

    /**
     * 通过执行对象获取流程变量渠道
     *
     * @param delegateTask
     *            执行对象
     * @return 请求渠道
     */
    public String getChannel(DelegateTask delegateTask) {
        return getVariableInstance(delegateTask, WorkflowConstants.CHANNEL_SIGNAL);
    }

    /**
     * 通过执行对象获取流程变量操作人
     *
     * @param delegateTask
     *            执行对象
     * @return 操作人id
     */
    public String getOperationUserId(DelegateTask delegateTask) {
        return getVariableInstance(delegateTask, WorkflowConstants.OPERATION_USER_ID);
    }

    /**
     * 通过执行对象获取流程变量流程发起人
     *
     * @param delegateTask
     *            执行对象
     * @return 流程发起人id
     */
    public String getStartUserId(DelegateTask delegateTask) {
        return getVariableInstance(delegateTask, WorkflowConstants.START_USER_ID);
    }

}
